package com.eastfair.projectcore.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.eastfair.boot.controller.SuperController;
import com.eastfair.core.base.R;
import com.eastfair.projectcore.dto.AreaPageQuery;
import com.eastfair.projectcore.dto.AreaSaveDTO;
import com.eastfair.projectcore.dto.AreaUpdateDTO;
import com.eastfair.projectcore.entity.Area;
import com.eastfair.projectcore.service.AreaService;
import com.eastfair.projectcore.vo.AreaVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * <p>
 * 前端控制器
 *
 * </p>
 *
 * @author zjn
 * @date 2021-08-23
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/area")
@Api(value = "Area", tags = "")
public class AreaController extends SuperController<AreaService, Integer, Area, AreaPageQuery, AreaSaveDTO, AreaUpdateDTO> {

    @Autowired
    private AreaService areaService;

    @Override
    public void handlerResult(IPage<Area> page) {
        // 想让返回值实现自动回显，请将此行代码打开
        // echoService.action(page);
    }

    /**
     * Excel导入后的操作
     *
     * @param list
     */
    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list) {
        List<Area> areaList = list.stream().map((map) -> {
            Area area = Area.builder().build();
            //TODO 请在这里完成转换
            return area;
        }).collect(Collectors.toList());

        return R.success(baseService.saveBatch(areaList));
    }

    /**
     * 查询某地区电话区号
     *
     * @param id 地区id
     * @return
     */
    @ApiOperation(value = "查询某地区电话区号", notes = "查询某地区电话区号")
    @GetMapping("/getPhoneCodeByAreaId/{id}")
    public R<String> getPhoneCodeByAreaId(@PathVariable("id") Integer id) {
        return R.success(areaService.getPhoneCodeByAreaId(id));
    }

    /**
     * 根据区域级别查询地区列表
     *
     * @param level 区域级别
     * @return 地区列表
     */
    @ApiOperation(value = "根据区域级别查询区域列表", notes = "根据区域级别查询区域列表")
    @GetMapping("/getByLevel/{level}")
    public R<List<AreaVO>> getByLevel(@PathVariable("level") String level) {
        return areaService.getByLevel(level);
    }

    /**
     * 根据父级id查询一级子集
     *
     * @param parentId 父级id
     * @return 一级子集地区列表
     */
    @ApiOperation(value = "根据父级id查询一级子集", notes = "根据父级id查询一级子集")
    @GetMapping("/getByParent/{parentId}")
    public R<List<AreaVO>> getByParent(@PathVariable("parentId") Long parentId) {
        return areaService.getByParent(parentId);
    }
    /**
     * 查询list
     *
     * @param area 区域级别
     * @return 地区列表
     */
    @ApiOperation(value = "查询list", notes = "查询list")
    @PostMapping("/queryListArea")
    public R<List<AreaVO>> queryListArea(@RequestBody Area area) {
        return R.success(areaService.queryList(area));
    }

}
